package com.shiro.distribution;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;

import java.util.Collection;
import java.util.Set;

/**
 * 功能或描述：分布式缓存接口
 *
 * @Package com.shiro.distribution
 * @Author: DR.YangLong
 * @Date: 14-9-30
 * @Time: 上午9:50
 * @Email: 410357434@163.com
 * @Version: 1.0
 * @Module: 修正:            日期：
 */
public class DistibuteCache<K, V> implements Cache<K, V> {
    private static final String DEFAULT_CACHE_NAME="dr_redis_cache:";
    /**
     * cache dao
     */
    private DistributeCacheRpository<K, V> distributeCacheRpository;
    /**
     * the name of this cache
     */
    private String name=DEFAULT_CACHE_NAME;

    public DistibuteCache() {
    }

    public DistibuteCache(DistributeCacheRpository<K, V> distributeCacheRpository) {
        this.distributeCacheRpository = distributeCacheRpository;
    }

    public DistibuteCache(DistributeCacheRpository<K, V> distributeCacheRpository, String name) {
        this.distributeCacheRpository = distributeCacheRpository;
        this.name = name;
    }

    @Override
    public V get(K key) throws CacheException {
        return distributeCacheRpository.get(key);
    }

    @Override
    public V put(K key, V value) throws CacheException {
        return distributeCacheRpository.add(key,value);
    }

    @Override
    public V remove(K key) throws CacheException {
        return distributeCacheRpository.delete(key);
    }

    @Override
    public void clear() throws CacheException {
        distributeCacheRpository.clear();
    }

    @Override
    public int size() {
        return distributeCacheRpository.getSize();
    }

    @Override
    public Set<K> keys() {
        return distributeCacheRpository.getKeys();
    }

    @Override
    public Collection<V> values() {
        return distributeCacheRpository.getValues();
    }

    public DistributeCacheRpository<K, V> getDistributeCacheRpository() {
        return distributeCacheRpository;
    }

    public void setDistributeCacheRpository(DistributeCacheRpository<K, V> distributeCacheRpository) {
        this.distributeCacheRpository = distributeCacheRpository;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
